import { Button, Upload, message, Modal } from "antd";
import type { DisplayItem } from "./schema.ts";
import {
  downloadBidFile,
  downloadBargainingFile,
  uploadBargainingFile,
  deleteBargainingFile,
  deleteBidFile,
} from "../../api/tendering/bidApi";
import React from "react";
import { useState } from "react";

interface BidActionButtonsProps {
  bidRecord: DisplayItem;
  onUploading?: (isUploading: boolean) => void;
}

const BidActionButtons: React.FC<BidActionButtonsProps> = ({
  bidRecord,
  onUploading,
}) => {
  const [loading, setLoading] = useState(false);
  const bidId: string = bidRecord.sourceId;

  async function handleUploadBargaining(file: any) {
    if (onUploading) {
      setLoading(true);
      onUploading(true);
    }
    try {
      await uploadBargainingFile(bidId, file);
      message.success("议价文件上传成功");
    } catch (error) {
      message.error("上传议价文件失败");
      console.error("上传议价文件失败:", error);
    } finally {
      setLoading(false);
      if (onUploading) onUploading(false);
    }
  }

  async function handleDownload(): Promise<void> {
    try {
      setLoading(true);
      if (bidRecord.type === "BID") {
        await downloadBidFile(bidId);
      } else {
        await downloadBargainingFile(bidId);
      }
    } catch (error) {
      message.error("下载文件失败");
      console.error("下载文件失败:", error);
    } finally {
      setLoading(false);
    }
  }

  async function handleDelete(): Promise<void> {
    Modal.confirm({
      title: "确认删除",
      content: "确定要删除该文件和相关记录吗？此操作不可恢复。",
      okText: "确认",
      cancelText: "取消",
      onOk: async (): Promise<void> => {
        try {
          setLoading(true);
          if (onUploading) onUploading(true);
          if (bidRecord.type === "BID") {
            await deleteBidFile(bidId);
          } else {
            await deleteBargainingFile(bidId);
          }
          message.success("删除成功");
        } catch (error) {
          message.error("删除文件失败");
          console.error("删除文件失败:", error);
        } finally {
          setLoading(false);
          if (onUploading) onUploading(false);
        }
      },
    });
  }

  return (
    <>
      {bidRecord.type === "BID" && (
        <Upload
          showUploadList={false}
          customRequest={({ file }): Promise<void> => {
            console.log("Uploading bargaining file:", file);
            return handleUploadBargaining(file);
          }}
          accept=".xlsx,.xls"
          maxCount={1}
        >
          <Button
            loading={loading}
            style={{ marginRight: "8px" }}
            type="default"
            size="small"
          >
            上传/修改议价
          </Button>
        </Upload>
      )}
      <Button
        loading={loading}
        onClick={(): Promise<void> => handleDownload()}
        style={{ marginRight: "8px" }}
        type="default"
        size="small"
      >
        下载
      </Button>
      <Button
        loading={loading}
        onClick={(): Promise<void> => handleDelete()}
        danger
        type="default"
        size="small"
      >
        删除
      </Button>
    </>
  );
};

export default BidActionButtons;
